package com.xintonghua.bussiness.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.SimpleBaseAdapter;
import com.xintonghua.bussiness.util.ImageUtils;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ChoosePhotoListViewAdapter extends SimpleBaseAdapter<String> {

    private AdapterClickListener adapterClickListener;

    public ChoosePhotoListViewAdapter(List<String> list, Context context) {
        super(list, context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_grid_photo, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!mList.get(position).contains("/")) {
            if (mList.get(position).equals("add")) {
//                sizeUtils.setLayoutSizeHeight12(holder.image);
                ImageUtils.displayRoundedImage(mList.get(position), holder.image, R.drawable.xiangji);
                holder.delete.setVisibility(View.GONE);
            } else {
                ImageUtils.displayRoundedImage(mList.get(position), holder.image);
                holder.delete.setVisibility(View.VISIBLE);
            }
        } else {
            ImageUtils.displayRoundedImagePhoto(mList.get(position), holder.image);
            holder.delete.setVisibility(View.VISIBLE);
        }


        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapterClickListener.deletePhoto(position);
            }
        });

        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.img_photo)
        ImageView image;
        @BindView(R.id.img_delete)
        ImageView delete;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 点击删除
     *
     * @param position
     */
    private void delete(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 获取图片张数
     *
     * @return
     */
    public int getPhotoCount() {
        return mList.size();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addAllData(List<String> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public interface AdapterClickListener {
        void deletePhoto(int pos);
    }

    public void setAdapterClickListener(AdapterClickListener adapterClickListener) {
        this.adapterClickListener = adapterClickListener;
    }

}
